package hust.hiepdv.javaproject.view;

import hust.hiepdv.javaproject.constant.Constant;
import hust.hiepdv.javaproject.model.Ball;
import hust.hiepdv.javaproject.model.Pad;
import hust.hiepdv.javaproject.networking.TCPClient;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ClientView implements Constant{

	private JFrame frame;
	private JTextField player1_score;
	private JTextField player2_score;
	private JTextField player3_score;
	private JTextField player4_score;
	JButton btnLeave;
	public JButton btnJoinGame;
	
	public static Ball ball = new Ball(BALL_INIT_X, BALL_INIT_Y, Ball.STYLE_1);
	public static Pad pad1 = new Pad(PAD1_INIT_X, PAD1_Y, Pad.STYLE_1, Pad.HORIZONTAL);
	public static Pad pad2 = new Pad(PAD2_X, PAD2_INIT_Y, Pad.STYLE_2, Pad.VERTICAL);
	public static Pad pad3 = new Pad(PAD3_INIT_X, PAD3_Y, Pad.STYLE_3, Pad.HORIZONTAL);
	public static Pad pad4 = new Pad(PAD4_X, PAD4_INIT_Y, Pad.STYLE_4, Pad.VERTICAL);
	
	TCPClient tcpClient;
	private JPanel panel;
	private JPanel panel_1;
	private JPanel panel_2;
	private JPanel panel_3;
	private JPanel panel_4;
	public JTextField tbServerIP;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientView window = new ClientView();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ClientView() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame("Client");
		frame.setResizable(false);
		frame.setBounds(100, 100, 770, 650);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel.setBounds(5, 8, 600, 600);
		frame.getContentPane().add(panel);
		
		btnJoinGame = new JButton("Join Game");
		btnJoinGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					tcpClient = new TCPClient(ClientView.this);
					Thread thread = new Thread(tcpClient);
					thread.start();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		btnJoinGame.setBounds(634, 117, 111, 34);
		frame.getContentPane().add(btnJoinGame);
		
		JLabel label = new JLabel("Player1");
		label.setBounds(613, 173, 46, 14);
		frame.getContentPane().add(label);
		
		JLabel label_1 = new JLabel("Player2");
		label_1.setBounds(613, 203, 46, 14);
		frame.getContentPane().add(label_1);
		
		JLabel label_2 = new JLabel("Player3");
		label_2.setBounds(613, 233, 46, 14);
		frame.getContentPane().add(label_2);
		
		JLabel label_3 = new JLabel("Player4");
		label_3.setBounds(613, 263, 46, 14);
		frame.getContentPane().add(label_3);
		
		player1_score = new JTextField();
		player1_score.setColumns(10);
		player1_score.setBounds(704, 173, 50, 20);
		frame.getContentPane().add(player1_score);
		
		player2_score = new JTextField();
		player2_score.setColumns(10);
		player2_score.setBounds(704, 203, 50, 20);
		frame.getContentPane().add(player2_score);
		
		player3_score = new JTextField();
		player3_score.setColumns(10);
		player3_score.setBounds(704, 233, 50, 20);
		frame.getContentPane().add(player3_score);
		
		player4_score = new JTextField();
		player4_score.setColumns(10);
		player4_score.setBounds(704, 263, 50, 20);
		frame.getContentPane().add(player4_score);
		
		btnLeave = new JButton("Leave ");
		btnLeave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				tcpClient.stop();
				ClientView.this.btnJoinGame.setEnabled(true);
				reset();
			}
		});
		btnLeave.setBounds(634, 328, 108, 34);
		frame.getContentPane().add(btnLeave);
		
		panel_1 = new JPanel();
		panel_1.setBackground(Color.ORANGE);
		panel_1.setBounds(669, 173, 20, 20);
		frame.getContentPane().add(panel_1);
		
		panel_2 = new JPanel();
		panel_2.setBackground(Color.RED);
		panel_2.setBounds(669, 203, 20, 20);
		frame.getContentPane().add(panel_2);
		
		panel_3 = new JPanel();
		panel_3.setBackground(Color.GREEN);
		panel_3.setBounds(669, 232, 20, 20);
		frame.getContentPane().add(panel_3);
		
		panel_4 = new JPanel();
		panel_4.setBackground(Color.MAGENTA);
		panel_4.setBounds(669, 262, 20, 20);
		frame.getContentPane().add(panel_4);
		
		tbServerIP = new JTextField();
		tbServerIP.setBounds(629, 70, 116, 20);
		frame.getContentPane().add(tbServerIP);
		tbServerIP.setColumns(10);
		
		JLabel lblServerip = new JLabel("ServerIP");
		lblServerip.setBounds(661, 45, 46, 14);
		frame.getContentPane().add(lblServerip);
	}
	public JPanel getPanel() {
		return panel;
	}

	public JFrame getFrame() {
		return frame;
	}

	public void setFrame(JFrame frame) {
		this.frame = frame;
	}
	
	public void updateScore(int p1, int p2, int p3, int p4){
		this.player1_score.setText(p1+"");
		this.player2_score.setText(p2+"");
		this.player3_score.setText(p3+"");
		this.player4_score.setText(p4+"");
	}
	
	public void reset(){
		ball.setData(BALL_INIT_X, BALL_INIT_Y, Ball.STYLE_1);
		pad1.setData(PAD1_INIT_X, PAD1_Y, Pad.STYLE_1, Pad.HORIZONTAL);
		pad2.setData(PAD2_X, PAD2_INIT_Y, Pad.STYLE_2, Pad.VERTICAL);
		pad3.setData(PAD3_INIT_X, PAD3_Y, Pad.STYLE_3, Pad.HORIZONTAL);
		pad4.setData(PAD4_X, PAD4_INIT_Y, Pad.STYLE_4, Pad.VERTICAL);				
	}
}
